package com.yn.xgame.logic.player;

import java.util.List;
import java.util.stream.Collectors;

import com.yn.xgame.logic.GamePlayer;
import com.yn.xgame.message.C2GPbRedirectMessage;
import com.yn.xgame.pb.GateClientProto;
import com.yn.xgame.pb.MessageIdProto;

public class UserMessageModule {
	GamePlayer gamePlayer;
	@Override
	public String toString() {
		return "UserMessageModule [gamePlayer=" + gamePlayer + ", list=" + list + "]";
	}
	private List<UserMessage> list;
	

	public List<UserMessage> getList() {
		return list;
	}
	public void setList(List<UserMessage> list) {
		this.list = list;
	}
	public UserMessageModule(GamePlayer gamePlayer, List<UserMessage> list) {
		super();
		this.gamePlayer = gamePlayer;
		this.list = list;
	}
   // 查询消息
	public void handleQueryMsg(C2GPbRedirectMessage msg) {
		GateClientProto.G2CMessage resp = GateClientProto.G2CMessage.newBuilder()
				.setMsgid(MessageIdProto.MessageId.G2C_UserMsgResp)
				.setUserMsg(GateClientProto.UserMsgResp.newBuilder().addAllMsg(list.stream()
						.filter(UserMessage::isDelete).map(UserMessage::build).collect(Collectors.toList())))
				.build();
		gamePlayer.respToClient(msg.getFromGateTopic(), resp);
	}
    //删除消息
	public void deleteMsg(C2GPbRedirectMessage msg) {
		List<String> deleteIdList = msg.getPb().getUserDeleteMsg().getMsgIdList();
		GateClientProto.G2CMessage resp = null;
		try {
			list.forEach(um -> {
				if (deleteIdList.contains(um.getMsg_id())) {
					um.setMsg_isread("-1");
				}
			});
			resp = GateClientProto.G2CMessage.newBuilder()
					.setMsgid(MessageIdProto.MessageId.G2C_DeleteUserMsgResp)
					.setDeleteResultCode(GateClientProto.UserDeleteMsgResp.newBuilder()
							.setResultCode(1))
					.build();
		} catch (Exception e) {
			resp = GateClientProto.G2CMessage.newBuilder()
					.setMsgid(MessageIdProto.MessageId.G2C_DeleteUserMsgResp)
					.setDeleteResultCode(GateClientProto.UserDeleteMsgResp.newBuilder()
							.setResultCode(0))
					.setWarning("删除信息失败！")
					.build();
		}
		gamePlayer.respToClient(msg.getFromGateTopic(), resp);
	}
	
	//已读信息
	public void handleReadMsg(C2GPbRedirectMessage msg) {
		List<String> msgid=msg.getPb().getReadMsgReq().getMsgIdList();
		GateClientProto.G2CMessage resp = null;
		try {
			list.forEach(um -> {
				if (msgid.contains(um.getMsg_id())) {
					um.setMsg_isread("1");
				}
			});
			resp = GateClientProto.G2CMessage.newBuilder()
					.setMsgid(MessageIdProto.MessageId.G2C_ReadMsgResp)
					.setReadMsgResp(GateClientProto.ReadMsgResp.newBuilder()
							.setResultCode(1))
					.setWarning("标志为已读")
					.build();
		} catch (Exception e) {
			resp = GateClientProto.G2CMessage.newBuilder()
					.setMsgid(MessageIdProto.MessageId.G2C_ReadMsgResp)
					.setReadMsgResp(GateClientProto.ReadMsgResp.newBuilder()
							.setResultCode(0))
					.setWarning("标志为已读失败")
					.build();
		}
		gamePlayer.respToClient(msg.getFromGateTopic(), resp);
	}

}
